package com.sqfw.project.wpReport.service.impl;


import cn.hutool.core.thread.ThreadUtil;
import com.github.pagehelper.PageInfo;
import com.sqfw.common.constant.HttpStatus;
import com.sqfw.common.exception.base.BaseException;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.mapper.SysUserMapper;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.domain.WpVisitInfo;
import com.sqfw.project.wp.mapper.WpInfoMapper;
import com.sqfw.project.wp.mapper.WpVisitInfoMapper;
import com.sqfw.project.wpReport.domain.SeatWorkReport;
import com.sqfw.project.wpReport.domain.WpInfoDetail;
import com.sqfw.project.wpReport.domain.dto.SeatWorkDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sqfw.project.wpReport.service.ISeatWorkReportService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

/**
 * 坐席工作情况表Service业务层处理
 *
 * @author lx
 * @date 2023-07-26
 */
@Slf4j
@Service
public class SeatWorkReportServiceImpl implements ISeatWorkReportService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private WpInfoMapper wpInfoMapper;

    @Autowired
    private WpVisitInfoMapper wpVisitInfoMapper;

    //业务组部门id
    private static final Long deptId = 101L;


    @Override
    public TableDataInfo getSeatWorkReportList(SeatWorkReport seatWorkReport) {
        List<SeatWorkReport> seatWorkReports = new ArrayList<>();
        SysUser sysUser = new SysUser();
        sysUser.setCode(seatWorkReport.getCode());
        sysUser.setNickName(seatWorkReport.getNickName());
        sysUser.setDeptId(deptId);
        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(new PageInfo(sysUsers).getTotal());
        //未查询到对应坐席人员,直接返回
        if (sysUsers.size() == 0) {
            tableDataInfo.setRows(seatWorkReports);
            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("查询成功");
            return tableDataInfo;
        }

        CountDownLatch countDownLatch = ThreadUtil.newCountDownLatch(sysUsers.size());
        for (SysUser user : sysUsers) {

            ThreadUtil.execute(() -> {

                WpInfo wpInfo = new WpInfo();
                wpInfo.setBegainTime(seatWorkReport.getBegainTime());
                wpInfo.setEndTime(seatWorkReport.getEndTime());
                wpInfo.setSender(user.getCode());

                List<WpInfo> wpInfosCon = wpInfoMapper.countWpInfoAll(wpInfo);

                WpVisitInfo wpVisitInfo = new WpVisitInfo();
                wpVisitInfo.setBegainTime(seatWorkReport.getBegainTime());
                wpVisitInfo.setEndTime(seatWorkReport.getEndTime());
                try {

                    /*List<WpInfo> wpInfos = wpInfosCon.stream()
                            .filter(infoCon -> user.getCode().equals(infoCon.getSender()))
                            .collect(Collectors.toList());*/
                    SeatWorkReport seatWorkReportCon = new SeatWorkReport();
                    seatWorkReportCon.setCode(user.getCode());
                    seatWorkReportCon.setNickName(user.getNickName());
                    seatWorkReportCon.setCreateWpCount(wpInfosCon.size());
                    //自办件
                    List<WpInfo> selfCount = wpInfosCon.stream()
                            .filter(info -> "自办".equals(info.getEndState()))
                            .collect(Collectors.toList());
                    seatWorkReportCon.setSelfCount(selfCount.size());
                    //流转
                    List<WpInfo> moveCount = wpInfosCon.stream()
                            .filter(info -> "流转".equals(info.getEndState()))
                            .collect(Collectors.toList());
                    seatWorkReportCon.setMoveCount(moveCount.size());


                    List<SeatWorkDTO> seatWorkDTOS = wpInfoMapper.selectWpInfoListByReport(wpInfo);
                    for (SeatWorkDTO seatWorkDTO : seatWorkDTOS) {
                        if ("工单归档".equals(seatWorkDTO.getNextState())) {
                            seatWorkReportCon.setEndCount(seatWorkDTO.getCountNumber());
                        }

                        if ("督导处理".equals(seatWorkDTO.getNextState())) {
                            seatWorkReportCon.setDdCount(seatWorkDTO.getCountNumber());
                        }

                        if ("质检审核处理".equals(seatWorkDTO.getNextState())) {
                            seatWorkReportCon.setZjCount(seatWorkDTO.getCountNumber());
                        }

                        if ("培训助理处理".equals(seatWorkDTO.getNextState())) {
                            seatWorkReportCon.setTrainCount(seatWorkDTO.getCountNumber());
                        }
                        /*if ("承办单位待处理".equals(seatWorkDTO.getNextState())){
                            seatWorkReportCon.setMoveCount(seatWorkDTO.getCountNumber());
                        }*/
                    }
                    if (seatWorkReportCon.getEndCount() == null) {
                        seatWorkReportCon.setEndCount(0);
                    }

                    if (seatWorkReportCon.getDdCount() == null) {
                        seatWorkReportCon.setDdCount(0);
                    }
                    if (seatWorkReportCon.getZjCount() == null) {
                        seatWorkReportCon.setZjCount(0);
                    }
                    if (seatWorkReportCon.getTrainCount() == null) {
                        seatWorkReportCon.setTrainCount(0);
                    }

                    if (seatWorkReportCon.getMoveCount() == null) {
                        seatWorkReportCon.setMoveCount(0);
                    }

                    //成功回访
                    wpVisitInfo.setAcceptVisit("1");
                    wpVisitInfo.setVisitWorkNo(user.getCode());
                    Integer visitSucessCount = wpVisitInfoMapper.countWpVisitInfoGroupByVisitWorkNo(wpVisitInfo);
                    //List<String> visitSucessCount = wpVisitInfoMapper.selectVisitSucessCount(wpVisitInfo);
                    seatWorkReportCon.setVisitSucessCount(visitSucessCount);
                    seatWorkReports.add(seatWorkReportCon);

                } catch (BaseException e) {
                    log.info("坐席-工单操作数据报表报错:{},{}",user.getUserId(),e.toString());
                } finally {
                    log.info("坐席-工单操作数据报表线程开启:{}",user.getUserId());
                    //调用线程计数器-1
                    countDownLatch.countDown();
                }
            });


        }
        //唤醒主线程
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            log.info("报错：{}",e.toString());
        }

        tableDataInfo.setRows(seatWorkReports);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
}
